package com.ygq.controller;

import DO.TeacherDO;
import DTO.AddStudentDTO;
import com.ygq.StudentService;
import com.ygq.TeacherService;
import com.ygq.purgeService;
import context.BaseContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import result.Result;

import java.io.File;

@RestController
@RequestMapping("/teacher")
@Slf4j
public class StudentController {
    @Autowired
    private StudentService studentService;

    @Autowired
    private purgeService purgeService;

    @Autowired
    private TeacherService teacherService;

    @Value("${ygq.path_excel}")
    private String excelPath;

    @Value("${ygq.path_ppt}")
    private String pptPath;

    @Value("${ygq.path_encode}")
    private String encodePath;

    @Value("${ygq.path_file_upload_test2}")
    private String fileUploadTestPath;

    /**
     * 添加学生
     */
    @PostMapping("/addStudent")
    public Result<String> addStudent(@RequestBody AddStudentDTO addStudentDTO){
        studentService.addStudent(addStudentDTO);
        return Result.success("添加成功");
    }

    /**
     * 重置密码
     */
    @GetMapping("/resetPassword")
    public Result<String> resetPassword(Integer id){
        studentService.resetPassword(id);
        return Result.success("ok");
    }

    /**
     * 删除学生
     */
    @DeleteMapping("/deleteStudent")
    public Result<String> deleteStudent(Integer id){
        studentService.deleteStudent(id);
        return Result.success("删除成功");
    }

    /**
     * 清除当年学生数据，只保留题目数据即可（也就是全部学生数据）
     * @return
     */
    @GetMapping("/clearStudent")
    public Result<String> clearStudent(){
        Integer currentId = BaseContext.getCurrentId();// 获取教师id
        Integer teacherByEleve = teacherService.getTeacherByEleve();
        if (!teacherByEleve.equals(currentId)){
            // 表示不是主教师在操作，返回无权限
            return Result.error("无权限",201);
        }
        log.info("删除全部学生数据");
        // 删除表数据
        purgeService.clear();

        // 删除文件数据
        // 1 删除class_excel
        deleteFilesInDirectory(excelPath);
        // 2 删除 encode
        deleteFilesInDirectory(encodePath);
        // 3 file_upload_test2
        deleteFilesInDirectory(fileUploadTestPath);
        // 4 ppt
        deleteFilesInDirectory(pptPath);
        return Result.success("ok");
    }


    // 删除文件夹下所有数据
    public static void deleteFilesInDirectory(String directoryPath) {
        File directory = new File(directoryPath);
        if (directory.exists() && directory.isDirectory()) {
            File[] files = directory.listFiles();
            if (files != null) {
                for (File file : files) {
                    if (!file.isDirectory()) { // 只删除文件，不删除子文件夹
                        file.delete();
                        System.out.println("Deleted file: " + file.getName());
                    }
                }
            }
        } else {
            System.err.println("Directory does not exist or is not a directory: " + directoryPath);
        }
    }

}
